<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2019-10-30 21:29:17
 * @LastEditors: cxguo
 * @LastEditTime: 2019-12-12 13:33:50
 -->
<template>
  <div>
    <!-- dataObj: {{ dataObj }} -->
    <el-form
      ref="goodBase"
      :model="dataObj"
      :label-position="'right'"
      :rules="ruleValidator"
      label-width="60px"
      size="small"
      class="w400"
    >
      <el-form-item label="编号:" prop="code">
        <el-input v-model="dataObj.code" />
      </el-form-item>
      <el-form-item label="名称:" prop="name">
        <el-input v-model="dataObj.name" />
      </el-form-item>
      <el-form-item label="分类:" prop="catName">
        <el-input v-model="dataObj.catName" disabled class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="btnSelectType" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="!multi.unit" label="单位:" prop="unit">
        <el-select v-model="dataObj.unit" filterable placeholder="请选择">
          <el-option
            v-for="item in optionsData.unit"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-checkbox v-model="multi.unit" border>多单位</el-checkbox>
      </el-form-item>
      <el-form-item v-if="!multi.specs" label="规格:" prop="specs">
        <el-input v-model="dataObj.specs">
          <template slot="append">
            <el-checkbox v-model="multi.specs">多规格</el-checkbox>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item v-if="!multi.barcode" label="条形码:" prop="barcode">
        <el-input v-model="dataObj.barcode" />
      </el-form-item>
    </el-form>
    <el-dialog title="请选择分类:" :visible.sync="dialogVisible" append-to-body>
      <CateTree ref="cateTree" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="btnGoodscatSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import { oftenuse } from '@/libs/mixins'
import CateTree from '@/views/goods/category'
import { list } from '@/api/goods/unit.js'
import { genGoodSpuCode } from '@/api/goods/goods-spu.js'

export default {
  name: 'GoodBase',
  components: { CateTree },
  mixins: [oftenuse],
  props: {
    multi: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      iframe: {},
      dialogVisible: false,
      optionsData: {
        unit: []
      },
      ruleValidator: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入商品编号', trigger: 'blur' }],
        catId: [{ required: true, message: '请选择所属类别', trigger: 'change' }],
        catName: [{ required: true, message: '请选择所属类别', trigger: 'change' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        specs: [{ required: true, message: '请输入规格', trigger: 'blur' }]
      }
    }
  },
  watch: {
  },
  created() {
    this.initData()
  },
  methods: {
    btnGoodscatSure() {
      const currentCat = this.$refs.cateTree.currentNode
      this.dialogVisible = false
      const { id, text } = currentCat
      this.$set(this.dataObj, 'catId', id)
      this.$set(this.dataObj, 'catName', text)
    },
    btnSelectType() {
      this.dialogVisible = true
    },
    // API
    resetData() {
      this.dataObj = {}
      this.$set(this.multi, 'specs', false)
      this.$set(this.multi, 'unit', false)
      this.initCode()
    },
    getUnit() {
      const value = this.dataObj.unit
      if (!value) return []
      const optionsData = this.optionsData.unit
      const result = optionsData.filter(item => { return item.value === value })
      this.$set(result[0], 'default', true)
      return result
    },
    initUnitData() {
      const pageInfo = this.pageInfo
      list({ data: {}, pageInfo }).then(res => {
        if (res.data.flag) {
          const data = res.data.data
          data.forEach(item => {
            item.value = item.id
            item.label = item.name
          })
          this.$set(this.optionsData, 'unit', data)
        }
      })
    },
    initCode() {
      genGoodSpuCode().then(res => {
        if (res.data.flag) {
          this.$set(this.dataObj, 'code', res.data.data)
        } else {
          this.$message.error('获取商品编号失败！')
        }
      })
    },
    initData() {
      this.initUnitData()
      this.initCode()
    }
  }
}
</script>
